package com.atguigu.flink.chapter10;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2023/6/27 14:20
 */
public class JdbcDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
    
//        env.enableCheckpointing(3000);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        tEnv.executeSql("create table sensor(" +
                            " id int, " +
                            " ts bigint, " +
                            " vc int " +
                            ")with(" +
                            "  'connector' = 'kafka', " +
                            "  'topic' = 's1', " +
                            "  'properties.bootstrap.servers' = 'hadoop162:9092', " +
                            "  'properties.group.id' = 'atguigu', " +
                            "  'scan.startup.mode' = 'latest-offset', " +
                            "  'format' = 'csv' " +
                            ")");
        
        Table table = tEnv.sqlQuery("select * from sensor");
        // table.execute().print();
        //....
        
        tEnv.executeSql("create table mysql_table(" +
                            " id int, " +
                            " ts bigint, " +
                            " vc int," +
                            " primary key(id) not enforced " +
                            ")with(" +
                            " 'connector' = 'jdbc'," +
                            " 'url' = 'jdbc:mysql://hadoop162:3306/flink?useSSL=false', " +
                            " 'table-name' = 'ws2', " +
//                            " 'sink.buffer-flush.max-rows' = '500'," +
//                            " 'sink.buffer-flush.interval' = '5s'," +
                            " 'username' = 'root', " +
                            " 'password' = 'aaaaaa' " +
                            ")");
        
        table.executeInsert("mysql_table");
        
        
    }
}
